var t = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    iconsUrl:t.globalData.iconsUrl,
    countList:[], 
    pageSize:10,
    list:[],
    page:1,// 当前页码
    last_page:1,
    totalCount:0,//总条数
    scrollHeight: null,
    no_more: false, // 没有更多数据
    isLoading: true, // 是否正在加载中
    isScrollAble:true,
    refreshOpen:true, //下拉刷新/下拉复位
    refreshstatus:true, //是否开启下拉刷新
    topNum:0, 
    search:"",
    //  
    userinfo:{}, 
    dataType:0,
    showStatus:{},
    couponText:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setListHeight()
    let that = this;
    that.setData({
      page:1,
      last_page:1,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
     
    });
    // 
    that.getDataList()
  },
  // 获取滚动条当前位置
  scrolltoupper:function(e){
  },
  //一键回到顶部
  backTop: function (e) {
    this.setData({topNum:0});
  },
  // 选项卡
  swichNav:function(e){
    this.setData({
      dataType:e.currentTarget.dataset.current,
      topNum:0,
      page:1,
      last_page:1,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
    });
    this.getDataList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  showDetail(e){
    let showStatus = this.data.showStatus
    showStatus[e.currentTarget.dataset.id] = !showStatus[e.currentTarget.dataset.id]
    this.setData({
      showStatus
    })
    console.log('showStatus',showStatus)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (t.globalData.show_tel_login) {
      t.showTelLogin()
      return;
    }
  },
  //获取数据列表
  getDataList(isPage, page){
    var that = this
    var o = {
      page: this.data.page,
      size: 10,
      type:this.data.dataType
    }
    t.request('/users/getCoupon',o,function(e){
        // 刷新状态复位
        that.setData({refreshOpen: 0 });
        // 不加这个方法真机下拉会一直处于刷新状态，无法复位
        if (e.code == "200") {
          let resList = e.result,dataList = that.data.list;
          let list = resList.list;
          let showStatus=that.data.showStatus
          list.forEach(element => {
            showStatus[element.user_coupon_id] = false
          });
          that.setData({
            showStatus,
            couponText:resList.describe
          })
          if (isPage == true) {
            that.setData({
              list:dataList.concat(list),
              isLoading: false,
              last_page: resList.total,
              totalCount: resList.total,
            })
          }else{
            that.setData({
              list,
              isLoading: false,
              last_page: resList.total,
              totalCount: resList.total,
            })
          }
        } else {
          wx.showToast({
            title: e.message,
            icon: 'none',
            duration: 2000
          })
        }
    })
  },
  // 订单结算
  leave(e){
    let store = e.currentTarget.dataset.store
    let site = e.currentTarget.dataset.site
    wx.navigateTo({
      url: '/pages/settle_accounts/settle_accounts?store='+store+'&site='+site,
    })
  },
  // 自定义下拉刷新
  upRefresh(){
    this.setData({
      list:[],
      page:1,// 当前页码
      last_page:1,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
    })
    this.getDataList()
  },
   /**
   * 下拉到底加载数据
   */
  bindDownLoad() {
    // 已经是最后一页
    console.log("翻页");
    console.log("page =",this.data.page)
    console.log("last_page =",this.data.last_page)
    if (this.data.page > this.data.last_page) {
      this.setData({
        no_more: true
      });
      return false;
    }
    // 加载下一页列表
    this.getDataList(true, ++this.data.page);
  },
  /**
   * 设置文章列表高度
   */
  setListHeight() {
    // let systemInfo = wx.getWindowInfo() 
    // console.log('systemInfo',systemInfo)
    // let screenHeight = systemInfo.windowHeight;
    // this.setData({
    //   scrollHeight: screenHeight - 160 - systemInfo.screenTop,
    // });
    const systemInfo = wx.getWindowInfo();
    console.log('systemInfo',systemInfo)
    let tabBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - systemInfo.statusBarHeight;
    // pad + windows
    if(systemInfo.pixelRatio <=2){
        // windows
        if(systemInfo.statusBarHeight ==0){
          tabBarHeight = (tabBarHeight-60)*systemInfo.pixelRatio
        }else{
          // pad
          // tabBarHeight = tabBarHeight*systemInfo.pixelRatio
          tabBarHeight = tabBarHeight-25
        }
    }else{
      tabBarHeight =(tabBarHeight+25)*systemInfo.pixelRatio
    }
    console.log('tabbar高度：',tabBarHeight+'rpx')
    // wx.showModal({
    //   title: '提示',
    //   content: tabBarHeight+'rpx'+JSON.stringify(systemInfo),
    // });
    this.setData({
        scrollHeight: tabBarHeight,
      });
/*
    let systemInfo = wx.getWindowInfo()
    let  rpx = systemInfo.windowWidth / systemInfo.windowHeight, // 计算rpx
    tapHeight = Math.floor(rpx * 250); // tap高度
      let windowHeight = parseInt(systemInfo.windowHeight - systemInfo.screenHeight + systemInfo.safeArea.bottom);
      let scrollHeight = windowHeight - tapHeight; // swiper高度
      console.log(systemInfo.windowHeight,scrollHeight);
      this.setData({
        scrollHeight
      });
  */
  },
  // 去洗车页面跳转
  golist(){
    wx.switchTab({
      url: '/pages/store/list',
    })
  },
  // 页面跳转
  goUrl(e){
    let url = e.currentTarget.dataset.url
    console.log(url)
    wx.navigateTo({
      url: url,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})